package TwoArray.reflect;


import TwoArray.entity.Person;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author feng
 * @Date 2021/6/19 20:47
 */
public class ReflectTest01 {
    public static void main(String[] args) {
        // 1.动态的加载类信息。
        Class classzz = Person.class;
        //   2.得到全类名
        String name = classzz.getName();
        System.out.println(name);
        Person p = new Person(1, "lisi", 20);
        try {
            // 1.动态的创建类对象
            Constructor cons = classzz.getConstructor(Integer.class, String.class, Integer.class);
            Person p1 = (Person) cons.newInstance(1, "lisi", 20);
           /* System.out.println(obj.toString());
            System.out.println(cons);*/
            // 2.通过反射获取它的属性
            Field field = classzz.getDeclaredField("age");
                  field.setAccessible(true);
                  field.set(p1,28);
            System.out.println(p1.toString());
            // 3.调方法
            Method method = classzz.getMethod("show");     //得到方法
            method.invoke(p1);    // 调用

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }


    }


}
